package com.xtoneict.auth.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xtoneict.auth.entity.Role;
import com.xtoneict.auth.mapper.PermissonMapper;
import com.xtoneict.auth.mapper.RoleMapper;
import com.xtoneict.auth.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    public List<Role> selectRolesByUserId(Integer userId){
        List<Integer> roleIds = roleMapper.selectRoleIdByUserId(userId);
        List<Role> roles = roleMapper.selectBatchIds(roleIds);
        return roles;
    }


    public boolean insertRoleAndUser(Integer userId, List<Integer> roleIds){
        for (Integer roleId : roleIds){
            roleMapper.insertUserAndRole(userId, roleId);
        }

        return true;
    }

    //删除某个用户对应的所有角色
    public boolean deleteUserAndRole(Integer userId){
        List<Integer> roleIds = roleMapper.selectRoleIdByUserId(userId);

        if(roleIds != null){
            for (Integer roleId : roleIds){
                roleMapper.deleteUserAndRole(userId, roleId);
            }
        }

        return true;
    }
}
